package com.pilipili.zeus.eduService.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.eduService.entity.EduChapter;
import com.pilipili.zeus.eduService.entity.EduVideo;
import com.pilipili.zeus.eduService.entity.chapter.Chapter;
import com.pilipili.zeus.eduService.entity.chapter.Video;
import com.pilipili.zeus.eduService.entity.excel.ChapterExcelData;
import com.pilipili.zeus.eduService.listener.ChapterListener;
import com.pilipili.zeus.eduService.mapper.EduChapterMapper;
import com.pilipili.zeus.eduService.service.EduChapterService;
import com.pilipili.zeus.eduService.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 番剧 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-26
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService videoService;

    /**
     * 查询章节、小节,chapterList存放结果,遍历,封装,存储
     * @param chapterId
     * @return chapterList
     */
    @Override
    public List<Chapter> getChapterVideoById(String chapterId) {
        //查询章节
        QueryWrapper<EduChapter> queryChapter = new QueryWrapper<>();
        queryChapter.eq("course_id",chapterId);
        List<EduChapter> chapters = baseMapper.selectList(queryChapter);
        //查询小节
        QueryWrapper<EduVideo> queryVideo = new QueryWrapper<>();
        queryVideo.eq("course_id",chapterId);
        queryVideo.orderByDesc("sort");
        List<EduVideo> videos = videoService.list(queryVideo);
        //存放结果
        List<Chapter> chapterList = new ArrayList<>();

        //遍历,封装,存储
        for (EduChapter eduChapter : chapters) {
            Chapter chapter = new Chapter();
            BeanUtils.copyProperties(eduChapter, chapter);
            chapterList.add(chapter);
            List<Video> videoList = new ArrayList<>();
            for (EduVideo vid:videos) {
                if (vid.getChapterId().equals(eduChapter.getId())) {
                    Video video = new Video();
                    BeanUtils.copyProperties(vid, video);
                    videoList.add(video);
                }
            }
            chapter.setChildren(videoList);
        }
        return chapterList;
    }

    /**
     * 按章节id删除
     * @param chapterId
     * @return true or false
     */
    @Override
    public boolean deleteById(String chapterId) {
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        int count = videoService.count(queryWrapper);
        if(count>0){
            throw new MyException(20001,"不能删除");
        }else {
            int i = baseMapper.deleteById(chapterId);
            return i>0;
        }
    }

    /**
     * 按番剧ID删除章节
     * @param courseId
     */
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_Id",courseId);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void saveChaptersDataByExcel(MultipartFile file, EduChapterService chapterService) {
        try {
            //文件输入流,调用方法读取Excel
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, ChapterExcelData.class,new ChapterListener(chapterService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
